<template>
  <div>
    <el-row>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-button type="primary" @click="add">新增</el-button>
          <el-button type="warning" @click="update" :disabled="select.length!=1">修改</el-button>
          <el-button type="danger" @click="onDelete" :disabled="select.length==0">删除</el-button>
        </div>
      </el-col>
      <el-col :span="8" :offset="4">
        <div class="grid-content bg-purple-light">
          <el-input
            placeholder="请输入内容"
            :clearable="true"
            
            prefix-icon="el-icon-search"
            v-model="input2"
            @keyup.enter.native="onSearch"
          ></el-input>
        </div>
      </el-col>
    </el-row>

    <div class="table">
      <el-table
        :data="tableData"
        height="300"
        @select="onSelectCB"
        @select-all="onAllSelect"
        :select-on-indeterminate="true"
        v-loading="loading"
        
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column  fixed prop="type" label="类别码" width="100"></el-table-column>
        <el-table-column   prop="area" label="区域" width="120"></el-table-column>
        <el-table-column  prop="author" label="发布者" width="70"></el-table-column>
        <el-table-column  prop="title" :show-overflow-tooltip="true" label="标题" width="150"></el-table-column>
        <el-table-column  prop="content" :show-overflow-tooltip="true" label="内容" width="200"></el-table-column>
        <el-table-column  prop="mobile" label="电话" width="150"></el-table-column>
        <el-table-column  prop="linkman" label="联系人" width="100"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" width="100">
            <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              style="margin-left:0"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      :current-page="curPage"
      @current-change="pageChange"
    ></el-pagination>
    <!-- 遮罩层 -->
    <div class="zhezhao" v-show="showModel">
      <el-row>
        <el-col :span="10" :offset="6">
          <div class="box" style="background:#fff">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>{{ruleForm.id?"修改":"新增"}}</span>
                <el-button style="float: right; padding: 3px 0" type="text" @click="close">关闭</el-button>
              </div>
              <div>
                <el-form
                  :model="ruleForm"
                  :rules="rules"
                  ref="ruleForm"
                  label-width="100px"
                  class="demo-ruleForm"
                >
                  <el-form-item label="区域" prop="area">
                    <el-input v-model="ruleForm.area"></el-input>
                  </el-form-item>
                  <el-form-item label="分类" prop="type">
                    <el-select v-model="ruleForm.type" placeholder="请选择分类">
                      <el-option
                        v-for="(item,index) in typeList"
                        :key="index"
                        :label="item.name"
                        :value="item.id"
                      >{{item.name}}</el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="发布者" prop="author">
                    <el-input v-model="ruleForm.author"></el-input>
                  </el-form-item>
                  <el-form-item label="标题" prop="title">
                    <el-input v-model="ruleForm.title"></el-input>
                  </el-form-item>
                  <el-form-item label="内容"  prop="content">
                    <el-input v-model="ruleForm.content" type="textarea"></el-input>
                  </el-form-item>
                  <el-form-item label="电话" prop="mobile">
                    <el-input v-model="ruleForm.mobile"></el-input>
                  </el-form-item>
                  <el-form-item label="联系人" prop="linkman">
                    <el-input v-model="ruleForm.linkman"></el-input>
                  </el-form-item>

                  <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm()">重置</el-button>
                  </el-form-item>
                </el-form>
              </div>
            </el-card>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { zyUrl, typeUrl } from "../utile/Url.js";
export default {
  data() {
    return {
      showModel: false,
      input2: "",
      tableData: [],
      select: [],
      rowId: "",
      total: 0,
      curPage: 1,
      pageSize: 5,
      loading:true,
      ruleForm: {
        area: "",
        type: "",
        author: "",
        title: "",
        content: "",
        mobile: "",
        linkman: ""
      },
      typeList: {},
      rules: {
        area: [{ required: true, message: "请输入区域", trigger: "blur" }],
        type: [{ required: true, message: "请选择分类", trigger: "change" }],
        author: [{ required: true, message: "请输入发布者", trigger: "blur" }],
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        linkman: [{ required: true, message: "请输入联系人", trigger: "blur" }]
      }
    };
  },
  methods: {
    //获取类型数据
    getTypeData() {
      
      axios({
        url: typeUrl,
        method: "get",
        params: {
          transform: 1
        }
      }).then(res => {
        // console.log(res)
        this.typeList = res.data.type;
       
      });
    },
    //获取数据
    getData() {
      this.loading=true
      axios({
        url: zyUrl,
        method: "get",
        params: {
          transform: 1,
          page: `${this.curPage},${this.pageSize}`,
          order: `id,desc`
        }
      }).then(res => {
        this.total = res.data._results;
        this.tableData = res.data.zydata;
         this.loading=false
      });
    },

    //当点击表格内修改按钮时
    handleEdit(index, row) {
      // console.log(index, row);

      this.showModel = true;
      this.ruleForm = row;
      axios({
        url: `${zyUrl}/${index}`,
        method: "put",
        data: this.ruleForm
      }).then(res => {
        this.getData();
        // this.ruleForm={}
      });
    },
    //当点击表格内删除按钮时
    handleDelete(index, row) {
      if (!confirm("确定要删除吗")) {
        return;
      }
      this.rowId = row.id;
      axios({
        url: zyUrl + "/" + this.rowId,
        method: "delete",
        data: row
      }).then(res => {
        this.getData();
      });
    },
    //点击提交按钮时
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // alert("submit!");
          // console.log(this.ruleForm.id);
          axios({
            url: this.ruleForm.id ? `${zyUrl}/${this.ruleForm.id}` : zyUrl,
            method: this.ruleForm.id ? "put" : "post",
            data: this.ruleForm
          }).then(res => {
            this.getData();
            this.showModel = false;
            this.ruleForm = {};
            this.resetForm()
          });
        } 
      });
    },
    //点击重置按钮时
    resetForm() {
      this.$refs['ruleForm'].resetFields();
    },
    //当点击全选时
    onAllSelect(selection) {
      this.select = selection;
      // console.log(selection)
    },
    //当多选按钮选择时
    onSelectCB(selection, row) {
      this.select = JSON.parse(JSON.stringify(selection));
      this.ruleForm =JSON.parse(JSON.stringify(selection[0])) || {};
      //  console.log(selection,row)
    },
    //当点击外部删除按钮时
    onDelete() {
      if (!confirm("确定要删除吗")) {
        return;
      }
      let ids = this.select.map(item => item.id);
      // console.log(ids.join(","))
      axios({
        url: zyUrl + "/" + ids.join(","),
        method: "delete"
      }).then(res => {
        this.getData();
        this.select = [];
      });
    },
    //点击外部修改按钮
    update(){
      this.showModel=true
      this.ruleForm = JSON.parse(JSON.stringify(this.ruleForm));

    },
    //当点击关闭时
    close() {
      this.showModel = false;
      this.select = [];
      this.ruleForm = {};
      this.resetForm()
    },
    //点击新增时
    add() {
      this.showModel = true;
      this.ruleForm = {};
      this.resetForm()

    },
    //点击分页页码时
    pageChange(currentpage) {
      // console.log(currentpage)
      this.curPage = currentpage;
      this.getData();
    },
    //搜索时
    onSearch(e,s) {
       this.loading=true
      axios({
        url: zyUrl,
        method: "get",
        params: {
          transform: 1,
          page: `${this.curPage},${this.pageSize}`,
          order: `id,desc`,
          filter: `title,cs,` + this.input2
        }
      }).then(res => {
        this.tableData = res.data.zydata;
        this.total = res.data._results;
         this.loading=false
      });
    }
  },
  mounted() {
    this.getData();
  },
  created() {
    this.getTypeData();
  }
};
</script>

<style>
.table {
  margin: 20px 0;
}
.zhezhao {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background: rgb(99, 97, 97);
  z-index: 99;
}
.box {
  margin: 15px 0;
}
</style>
